package com.lzw.cpvideoplayer.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzw.cpvideoplayer.R;
import com.lzw.cpvideoplayer.activity.ContentActivity;
import com.lzw.cpvideoplayer.data.TitleData;
import com.lzw.cpvideoplayer.ulit.OnItemClickLiner;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class MyBaseAdapter extends RecyclerView.Adapter<MyBaseAdapter.MyHolder> {
    public  List<TitleData> data;
    private Context mContext;
    private int count;
    public int count_max;
    private View view = null;
    private OnItemClickLiner clickLiner;
    private static final String TAG = "MyBaseAdapter";
    public MyBaseAdapter(List<TitleData> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
        count = 1;  }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        Log.e(TAG, "onCreateViewHolder: "+viewType );
        if(viewType!=0){
            return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.end_item_search,parent,false),clickLiner);
        }
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.item_data,parent,false),clickLiner);

    }
        public void setOnItemClickLiner(OnItemClickLiner clickLiner){
            this.clickLiner = clickLiner;
        }
    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        //  holder.image.setImageURI(Uri.parse(data.getData()[position].getCover()));
        if(data.get(position).getType() == 1){
                holder.count.setText("第"+count+"/");
            holder.count_max.setText(count_max+"页");
        }else {
            String imageCover =  data.get(position).getCover();
            Log.e(TAG, "搜索结果页: "+imageCover );
            Glide.with(mContext).load(imageCover).into(holder.image);
            //  Log.e(TAG, "getView: "+data.getData()[position].getCover());
            //  holder.image.setImageResource(cn.jzvd.R.drawable.jz_pause_pressed);
            holder.title.setText(data.get(position).getTitle());
            holder.director.setText("作者:"+data.get(position).getDirector());
            holder.descs.setText("简介:"+data.get(position).getDescs());
            //  Log.e(TAG, "getView:-"+http);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ContentActivity.class);
                    intent.putExtra("videoId",data.get(holder.getAdapterPosition()).getVideoId());
                    ContentActivity.searchdata = data;
                    ContentActivity.position = holder.getAdapterPosition();
                    mContext.startActivity(intent);
                    //  Log.e(TAG, "getView:-"+data.getData()[position].getVideoId());
                    //  OkHttpResult.LoadVoideoURL(mContext,data.getData()[position].getVideoId());

                }
            });
        }

    }
    public void notifyItem(List<TitleData> data,int count,int countMax){
        this.count = count;
       // count_max = countMax;
       this.data = data;
       notifyItemRangeRemoved(data.size(),10);
       notifyItemRangeChanged(0,data.size()-1);
//        notifyItemInserted(0);
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }


    @Override
    public int getItemCount() {
        return data.size();
    }


     static class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView image;
        private TextView title;
        private TextView director;//作者
        private TextView descs;//简介
         private TextView next;
         private TextView back;
         private TextView count;
         private TextView count_max;
        private OnItemClickLiner clickLiner;
        public MyHolder(@NonNull View itemView,OnItemClickLiner clickLiner) {
            super(itemView);
            this.clickLiner = clickLiner;
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.item_title);
           director = itemView.findViewById(R.id.item_director);
           descs = itemView.findViewById(R.id.item_descs);
            next = itemView.findViewById(R.id.next_count);
            back = itemView.findViewById(R.id.back_count);
            count = itemView.findViewById(R.id.count);
            count_max = itemView.findViewById(R.id.count_max);

            if(next!=null&&back!=null){
                next.setOnClickListener(this);
                back.setOnClickListener(this);
            }

        }

         @Override
         public void onClick(View v) {
             clickLiner.OnItemClick(v,getAdapterPosition());
         }
     }
}
